﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.DynamicData;
using System.Data.SqlClient;

namespace sistemaAcademicoFinanceiroEscolas.disciplinaInsert
{
    public partial class Insert : System.Web.UI.Page
    {
        protected MetaTable table;

        protected void Page_Init(object sender, EventArgs e)
        {
            DynamicDataManager1.RegisterControl(DetailsView1);
            Session["jadefiniufoco"] = "Nao"; 
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            table = DetailsDataSource.GetTable();
            Title = table.DisplayName;
        }

        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == DataControlCommands.CancelCommandName)
            {
                Response.Redirect(table.ListActionPath);
            }
        }

        protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception == null || e.ExceptionHandled)
            {

                string sqls = "select * from disciplina";
                System.Data.SqlClient.SqlConnection sqle = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                sqle.Open();
                System.Data.SqlClient.SqlCommand cmdListar = new System.Data.SqlClient.SqlCommand(sqls, sqle);
                System.Data.SqlClient.SqlDataReader drListar = cmdListar.ExecuteReader();

                string descricaoDisciplina = "";
                string codigoDisciplina = "";
                string descricaoDisciplinaMax = "";
                string codigoDisciplinaMax = "";
                
                if (drListar.HasRows)
                {
                    while (drListar.Read())
                    {
                        codigoDisciplina = drListar["codigoDisciplina"].ToString();
                        descricaoDisciplina = drListar["descricaoDisciplina"].ToString();

                        descricaoDisciplinaMax = descricaoDisciplinaMax.ToUpper();
                        descricaoDisciplina = descricaoDisciplina.ToUpper();

                        if (codigoDisciplina != codigoDisciplinaMax && descricaoDisciplina.Equals(descricaoDisciplinaMax))
                        {
                            SqlConnection sqlDelMaquina = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                            sqlDelMaquina.Open();

                            string sqlDeletar = "delete from disciplina where codigoDisciplina = " + codigoDisciplinaMax + "";

                            SqlCommand deletarDetalhe = new SqlCommand(sqlDeletar, sqlDelMaquina);
                            deletarDetalhe.ExecuteNonQuery();

                            sqlDelMaquina.Close();

                            Response.Redirect("Insert.aspx?informacao=sim");
                        }
                    }
                }

                cmdListar.Dispose();
                drListar.Close();

            
                   SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                    sqlc.Open();
                    SqlConnection sqld = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                    sqld.Open();

                    string select = "select MAX(codigoDisciplina) AS codigoDisciplina from disciplina";
                    SqlCommand cmd = new SqlCommand(select, sqlc);
                    SqlDataReader dr = cmd.ExecuteReader();

                    dr.Read();

                    string sql = "update disciplina set codigoSerie = 2 where codigoDisciplina = " + dr["codigoDisciplina"].ToString() + "";

                    SqlCommand up = new SqlCommand(sql, sqld);
                    up.ExecuteNonQuery();

                    sqlc.Close();
                    sqld.Close();

                    Response.Redirect(table.ListActionPath);
                }



            }
        }
    }

